package cn.tedu.hospitalqd.controller;

import cn.tedu.hospitalqd.pojo.dto.DrugAddNewDTO;
import cn.tedu.hospitalqd.pojo.dto.DrugUpdateDTO;
import cn.tedu.hospitalqd.pojo.vo.DrugListItemVO;
import cn.tedu.hospitalqd.pojo.vo.DrugStandardVO;
import cn.tedu.hospitalqd.service.DrugService;
import cn.tedu.hospitalqd.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/drug")
@Api(tags = "03. 药品模块")
@Validated
public class DrugController {

    @Autowired
    private DrugService drugService;

    @PostMapping("/add-new")
    @ApiOperation("添加药品")
    @ApiOperationSupport(order = 100)
    public JsonResult addNew(@Valid DrugAddNewDTO drugAddNewDTO){
        log.debug("开始处理【添加药品】的请求，参数：{}", drugAddNewDTO);
        drugService.addNew(drugAddNewDTO);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/delete")
    @ApiOperation("根据ID删除药品")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id",value = "药品ID", required = true,dataType = "long")
    })
    public JsonResult delete(@Range(min = 1,message = "删除药品失败，ID值无效 ")
                             @PathVariable Long id){
        log.debug("开始处理【【根据ID删除药品】的请求，参数：{}",id);
        drugService.delete(id);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/update")
    @ApiOperation("修改药品详情")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id",value = "药品ID", required = true,dataType = "long")
    })
    public JsonResult updateInfoById(@Range(min = 1,message = "修改药品详情失败，ID值无效 ")
                             @PathVariable Long id, DrugUpdateDTO drugUpdateDTO){
        log.debug("开始处理【修改药品详情】的请求，参数：{}", drugUpdateDTO);
        drugService.updateInfoById(id,drugUpdateDTO);
        return JsonResult.ok();
    }

    @GetMapping("/{id:[0-9]+}")
    @ApiOperation("根据ID查询药品详情")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id", value = "药品ID", required = true, dataType = "long")
    })
    public JsonResult getStandardById(@Range(min = 1, message = "获取药品详情失败，ID值无效！")
                                      @PathVariable Long id) {
        log.debug("开始处理【根据ID查询药品详情】的请求，参数：{}", id);
        DrugStandardVO queryResult = drugService.getStandardById(id);
        return JsonResult.ok(queryResult);
    }

    @GetMapping("")
    @ApiOperation("查询药品列表")
    @ApiOperationSupport(order = 420)
    public JsonResult list(){
        log.debug("根据ID查询药品详情");
        List<DrugListItemVO> list = drugService.list();
        return JsonResult.ok(list);
    }

}
